﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.fmtx.Model;
using fmtx.webUtility;
using com.fmtx.IDal;
using fmtx.Framework.DALFactory;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

    public class LoginModel
    {
        private static readonly string COOKIEKEY_USERID = "fmtx_website_cookie_userid";
        private static readonly string SESSIONKEY_USERINFO = "fmtx_website_userinfo";
        private static readonly string ENCRYPT = "h5$g_1!^";

        /// <summary>
        /// 用户信息
        /// </summary>
        public static UserInfo User 
        { 
            get
            {
                return getUserInfo();
            }
        }

        #region 将用户信息写入cookie和session
        /// <summary>
        /// 将用户信息写入cookie和session
        /// </summary>
        /// <param name="user"></param>
        /// <param name="expired"></param>
        public static void SetUserInfo(UserInfo user, double expired = 0)
        {
            WebCookieHelper.SetCookie(COOKIEKEY_USERID,ENCRYPT,user.Uid.ToString(), expired);
            HttpContext.Current.Session[SESSIONKEY_USERINFO] = user;
        } 
        #endregion

        #region 读取用户信息
        /// <summary>
        /// 读取用户信息
        /// </summary>
        /// <returns></returns>
        private static UserInfo getUserInfo()
        {
            UserInfo user = null;
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                user = context.Session == null ? null : context.Session[SESSIONKEY_USERINFO] as UserInfo;
                if (user == null)
                {
                    var cookiestr = WebCookieHelper.GetCookie(COOKIEKEY_USERID, ENCRYPT);
                    if (string.IsNullOrEmpty(cookiestr))
                        return new UserInfo();
                    var userId = Convert.ToInt32(cookiestr);
                    var iuser = DataAccess<IUserInfo>.GetObject();
                    user = iuser.Get(userId);
                    if (user == null)
                        return new UserInfo();
                    HttpContext.Current.Session[SESSIONKEY_USERINFO] = user;
                }
            }
            return user;
        } 
        #endregion
    }

    public class LogOnModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [Display(Name = "记住我?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "用户名")]
        [Remote("IsExsts", "User", "Action",ErrorMessage="用户名已被注册！")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "请输入正确的email地址")]
        [Remote("IsMailExsts", "User", "Action", ErrorMessage = "电子邮件地址已被注册！")]
        [Display(Name = "电子邮件地址")]
        public string Email { get; set; }

        [Required]
        [RegularExpression("[\u4e00-\u9fa50-9a-zA-Z_]{6,20}",ErrorMessage="密码必须为6-20位")]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    